package com.brainhealth.customer.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.annotation.RepeatSubmit;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.CustomerFeedback;
import com.brainhealth.customer.service.ICustomerFeedbackService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 会员反馈信息Controller
 * 
 * @author wolong
 * @date 2023-04-25
 */
@RestController
@RequestMapping("/customer/feedback")
public class CustomerFeedbackController extends BaseController
{
    @Autowired
    private ICustomerFeedbackService customerFeedbackService;

    /**
     * 查询会员反馈信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerFeedback customerFeedback)
    {
        startPage();
        List<CustomerFeedback> list = customerFeedbackService.selectCustomerFeedbackList(customerFeedback);
        return getDataTable(list);
    }

    /**
     * 导出会员反馈信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:export')")
    @Log(title = "会员反馈信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerFeedback customerFeedback)
    {
        List<CustomerFeedback> list = customerFeedbackService.selectCustomerFeedbackList(customerFeedback);
        ExcelUtil<CustomerFeedback> util = new ExcelUtil<CustomerFeedback>(CustomerFeedback.class);
        util.exportExcel(response, list, "会员反馈信息数据");
    }

    /**
     * 获取会员反馈信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:query')")
    @GetMapping(value = "/{customerFeedbackId}")
    public AjaxResult getInfo(@PathVariable("customerFeedbackId") Long customerFeedbackId)
    {
        return success(customerFeedbackService.selectCustomerFeedbackByCustomerFeedbackId(customerFeedbackId));
    }

    /**
     * 新增会员反馈信息
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:add')")
    @Log(title = "会员反馈信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerFeedback customerFeedback)
    {
        return toAjax(customerFeedbackService.insertCustomerFeedback(customerFeedback));
    }

    /**
     * 修改会员反馈信息
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:edit')")
    @Log(title = "会员反馈信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerFeedback customerFeedback)
    {
        return toAjax(customerFeedbackService.updateCustomerFeedback(customerFeedback));
    }

    /**
     * 删除会员反馈信息
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:remove')")
    @Log(title = "会员反馈信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerFeedbackIds}")
    public AjaxResult remove(@PathVariable Long[] customerFeedbackIds)
    {
        return toAjax(customerFeedbackService.deleteCustomerFeedbackByCustomerFeedbackIds(customerFeedbackIds));
    }

    /**
     * 审核会员反馈信息
     */
    @PreAuthorize("@ss.hasPermi('customer:feedback:edit')")
    @Log(title = "会员反馈信息", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult feedbackAudit(@RequestBody Map<String, Long> map)
    {
        Long feedbackId = map.get("feedbackId");
        int result = customerFeedbackService.customerFeedbackAudit(feedbackId);
        if (result > 0)
        {
            return AjaxResult.success("会员反馈信息审核成功！");
        }
        return AjaxResult.success("会员反馈信息审核成功！");
    }

    /**
     * 添加会员反馈信息
     * @param request
     * @param customerFeedback
     * @return
     */
    @Anonymous
    @PostMapping("/addCustomerFeedback")
    public AjaxResult addCustomerFeedback(HttpServletRequest request, @RequestBody CustomerFeedback customerFeedback)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        if (customer == null)
        {
            return AjaxResult.error("登录信息过期，请重新登录！");
        }
        customerFeedback.setCustomerId(customer.getCustomerId());
        customerFeedback.setCustomerName(customer.getCustomerName());
        if (customerFeedback.getContent().length() > 9)
        {
            customerFeedback.setFeedbackTitle(customerFeedback.getContent().substring(0, 9));
        } else
        {
            customerFeedback.setFeedbackTitle(customerFeedback.getContent());
        }
        int result = customerFeedbackService.insertCustomerFeedback(customerFeedback);
        if (result > 0)
        {
            return AjaxResult.success("会员反馈信息成功！");
        }
        return AjaxResult.error("会员反馈信息失败！");
    }
}
